package com.cskaoyan.shiro;

import com.cskaoyan.bean.BaseRespVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.web.filter.authc.UserFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author tangjian
 * @Date 2021/10/23 17:21
 * @description
 */
public class LoginFilter extends UserFilter {
    @Override
    protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {

        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        response.setContentType("application/json; charset=utf-8");
        // 自定义返回内容
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String requestURI = httpServletRequest.getRequestURI();

        if (requestURI.contains("admin")) {
            httpServletResponse.sendRedirect("http://localhost:8080/");
        }else if (requestURI.contains("wx")){
            ObjectMapper objectMapper = new ObjectMapper();

            //String fail = objectMapper.writeValueAsString(BaseRespVo.fail(501,"请登录"));

            response.getWriter().println("{\"errno\":501,\"errmsg\":\"请登录\"}");
        }
    }
}
